package org.example.spirngmvcdemo.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
public class LoginController {
    @RequestMapping("/check")
    public boolean check(String username, String password, HttpSession session) {
        // 校验用户名和密码
//        if(username == null || "".equals(username) || password == null || "".equals(password)) {
//            return false;
//        }
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        // 此处使用数据模拟 约定用户名为 zhangsan 密码为 123456
        if("zhangsan".equals(username) && "123456".equals(password)) {
            session.setAttribute("username", username);
            return true;
        }
        return false;
    }

    @RequestMapping("/index")
    public String index(HttpSession session) {
        String username =(String) session.getAttribute("username");
        return username;
    }
}
